package com.godofgamblers.hotelmanagementsystem.handler;

import com.godofgamblers.hotelmanagementsystem.dto.OfflineBookingDto;
import com.godofgamblers.hotelmanagementsystem.dto.OfflinePriceDto;
import com.godofgamblers.hotelmanagementsystem.pojo.Check_In_Info;
import com.godofgamblers.hotelmanagementsystem.pojo.Room;
import com.godofgamblers.hotelmanagementsystem.pojo.Room_Type;
import com.godofgamblers.hotelmanagementsystem.pojo.User;
import com.godofgamblers.hotelmanagementsystem.service.Check_In_InfoService;
import com.godofgamblers.hotelmanagementsystem.util.DateUtil;
import com.godofgamblers.hotelmanagementsystem.util.RegexUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 入住相关操作
 */
@Controller
@RequestMapping("/check_in")
public class Check_In_InfoHandler {
    @Autowired
    private Check_In_InfoService check_in_infoService;

    private ModelAndView modelAndView = new ModelAndView();

    /**
     * 添加实际入住人信息
     * @param check_in_info 入住信息pojo类
     * @param request   请求
     * @return  String
     */
    @RequestMapping("/addinfos")
    @ResponseBody
    public String Check_In_Infos(Check_In_Info check_in_info, HttpServletRequest request){
        HttpSession session = request.getSession();
        //获取session中的入住信息list
        List<Check_In_Info> Check_In_Infos= getCheckInInfos(request);
        //将入住信息添加进List<Check_In_Info>中
        Check_In_Infos.add(check_in_info);
        //将List<Check_In_Info>存进session中
        session.setAttribute("Check_In_Infos",Check_In_Infos);
        String result = "添加成功！";
        return result;
    }

    /**
     * 线上预定入住
     * @param request 请求
     * @return String
     */
    @RequestMapping("/Booking")
    @ResponseBody
    public OfflineBookingDto onlineBooking(Check_In_Info checkInInfo,HttpServletRequest request){
        OfflineBookingDto offlineBookingDto = new OfflineBookingDto();
        String result = "操作失败！";
        //检查数据合法性
        RegexUtil regexUtil = new RegexUtil();
        if(checkInInfo.getActual_name()==""||checkInInfo.getActual_name()==null){
            result = "请输入入住人姓名";
            offlineBookingDto.setResult(result);
            return offlineBookingDto;
        }
        if(!regexUtil.isIDCard(checkInInfo.getId_card())){
            result = "请输入有效的身份证号";
            offlineBookingDto.setResult(result);
            return offlineBookingDto;
        }
        //检查该入住信息是否可用
        //获取储存入住信息的List<Check_In_Info>
        List<Check_In_Info> check_in_infos=getCheckInInfos(request);
        if(checkInInfo.getActual_name()!=null&&checkInInfo.getId_card()!=null){
            check_in_infos.add(checkInInfo);
        }
        //遍历入住信息
        for ( Check_In_Info check_in_info:check_in_infos) {
            //开单
            result = check_in_infoService.addCheckInInfo(check_in_info);
        }
        HttpSession session = request.getSession();
        session.removeAttribute("Check_In_Infos");
        offlineBookingDto.setRoom_num(checkInInfo.getRoom_num());
        offlineBookingDto.setResult(result);
        return offlineBookingDto;
    }

    /**
     * 根据计划住房时间、房间类型、用户会员折扣查询房间并计算价格
     * @param offlinePriceDto 线下价格类 ，存放：计划入住时间、计划退房时间、房间类型id、房间状态、折扣、价格、房间信息list
     * @return  OfflinePriceDto  价格
     */
    @RequestMapping("/offlineShowRooms")
    @ResponseBody
    public OfflinePriceDto offlineShowRooms(OfflinePriceDto offlinePriceDto){
        OfflinePriceDto offlinePriceDtoResult = new OfflinePriceDto();
        if(DateUtil.calculateDays(offlinePriceDto.getStartTime(),new SimpleDateFormat("yyyy-MM-dd").format(new Date()))>0){
            offlinePriceDtoResult.setResult("不能选择今天之前的日期");
            return offlinePriceDtoResult;
        }
        if(DateUtil.calculateDays(offlinePriceDto.getStartTime(),offlinePriceDto.getEndTime())<=0){
            offlinePriceDtoResult.setResult("计划离店时间必须大于计划入住时间");
            return offlinePriceDtoResult;
        };
        offlinePriceDtoResult=check_in_infoService.offlineShowRooms(offlinePriceDto);
        return offlinePriceDtoResult;
    }

    /**
     * 获取储存入住信息的List<Check_In_Info>
     * @param request 请求
     * @return List<Check_In_Info>
     */
    public List<Check_In_Info> getCheckInInfos(HttpServletRequest request){
        //获取session对象
        HttpSession session = request.getSession();
        //创建一个list<Check_In_Info>，用于储存入住信息
        List<Check_In_Info> check_In_Infos= null;
        //获取session中的入住信息list
        Object object = session.getAttribute("Check_In_Infos");
        if(object==null){
            //如果object为null，新建一个 List<Check_In_Info>对象，并存入session中
            check_In_Infos=new ArrayList<Check_In_Info>();
            session.setAttribute("Check_In_Infos",check_In_Infos);
        }else{
            //如果object不为null，将object转换为List<Check_In_Info>
            check_In_Infos= (List<Check_In_Info>) object;
        }
        return check_In_Infos;
    }

    /**
     * 线下开单
     * @param user 用户pojo类,传入用户id
     * @param offlinePriceDto  线下价格pojo类，传入价格、房间类型id、计划入住时间、计划离店时间
     * @param check_in_info 入住信息pojo类、传入房间号、入住人姓名、身份证、实际入住时间
     * @return OfflineBookingDto 操作结果
     */
    @RequestMapping("/offlineBooking")
    @ResponseBody
    public OfflineBookingDto offlineBooking(User user,OfflinePriceDto offlinePriceDto,Check_In_Info check_in_info){
        OfflineBookingDto offlineBookingDtoResult = new OfflineBookingDto();
        //检查时间
        if(DateUtil.calculateDays(offlinePriceDto.getStartTime(),offlinePriceDto.getEndTime())<=0){
            offlineBookingDtoResult.setResult("计划离店时间必须大于计划入住时间");
            return offlineBookingDtoResult;
        };
        if(DateUtil.calculateDays(offlinePriceDto.getStartTime(),new SimpleDateFormat("yyyy-MM-dd").format(new Date()))>0){
            offlineBookingDtoResult.setResult("不能选择今天之前的日期");
            return offlineBookingDtoResult;
        }
        //检查数据合法性
        RegexUtil regexUtil = new RegexUtil();
        if(check_in_info.getActual_name()==""||check_in_info.getActual_name()==null){
            offlineBookingDtoResult.setResult("请输入入住人姓名");
            return offlineBookingDtoResult;
        };
        if(!regexUtil.isIDCard(check_in_info.getId_card())){
            offlineBookingDtoResult.setResult("请输入有效的身份证号");
            return offlineBookingDtoResult;
        };
        //将数据传入service层执行开单相关业务
        String result=check_in_infoService.offlineBooking(user,offlinePriceDto,check_in_info);
        offlineBookingDtoResult.setResult(result);
        offlineBookingDtoResult.setRoom_num(check_in_info.getRoom_num());
        return offlineBookingDtoResult;
    }

    /**
     * 退房
     * @param room 传入房间编号
     * @return String
     */
    @RequestMapping("/checkOut")
    @ResponseBody
    public String checkOut(Room room){
        String result = "操作失败！";
        //将房间编号传入service层执行退房相关业务
        result = check_in_infoService.checkOut(room);
        return result;
    }

    /**
     * 根据房间类型及状态显示房间
     * @param room  传入房间类型、房间状态
     * @return OfflinePriceDto 房间信息
     */
    @RequestMapping("/operateShowRoom")
    @ResponseBody
    public OfflinePriceDto operateShowRoom(Room room){
        //根据房间类型及状态显示房间
        OfflinePriceDto offlinePriceDto = check_in_infoService.operateShowRoom(room);
        return offlinePriceDto;
    }

    /**
     * 查询所有房间类型名称
     * @return List<Room_Type>
     */
    @RequestMapping("/findRoomTypeInfos")
    @ResponseBody
    public List<Room_Type> findRoomTypeInfos(){
        //查询所有房间类型及其对应id
        List<Room_Type> room_types = check_in_infoService.findRoomTypeInfos();
        return room_types;
    }
}
